﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;


namespace VIP_Shop
{
    class Class1
    {
   private DataTable table = new DataTable();
        private string strConnect =
            @"Data Source=DARK\SQLEXPRESS;Initial Catalog=Test;Integrated Security=True";    
        //private string strConnect;
        
        private SqlDataAdapter adap = new SqlDataAdapter();
        private SqlDataAdapter adap1 = new SqlDataAdapter();
        private SqlDataAdapter adap2 = new SqlDataAdapter();
        SqlConnection con;
            
        public Class1()
        {
            //string path = Application.StartupPath + @"\banh.mdf";
            //strConnect =
            //"Data Source=THI;Initial File Name="+path
            //    +";Integrated Security=True";
            con = new SqlConnection(strConnect);
        }
        //Select: Lấy dữ liệu vào một DataTable
        public DataTable GetData(string strSelect) {
            adap.SelectCommand = new SqlCommand(strSelect, con);
            adap.Fill(table);
            return table;
        }
        //Select: Lấy dữ liệu vào một DataTable, mo hinh phi ket noi
        public DataTable GetData1(string strSelect) {
            DataTable table = new DataTable();
            adap1.SelectCommand = new SqlCommand(strSelect, con);
            adap1.Fill(table);
            return table;
        }
        //Select: Lấy dữ liệu vào một DataTable, mo hinh phi ket noi
        public DataTable GetData2(string strSelect)
        {
            DataTable table = new DataTable();
            adap2.SelectCommand = new SqlCommand(strSelect, con);
            adap2.Fill(table);
            return table;
        }
        //insert,update,delete: them,sua,xoa ban ghi, mo hinh ket noi
        public void Update(string strUpdate) {
            SqlCommand com = new SqlCommand(strUpdate, con);
            con.Open();
            com.ExecuteNonQuery();
            con.Close();
          //  table.Clear();
          //  adap.Fill(table);
        }
        //Truy van tra ve mot gia tri, mo hinh ket noi
        public object GetOneData(string strUpdate)
        {
            SqlCommand com = new SqlCommand(strUpdate, con);
            con.Open();
            object obj = com.ExecuteScalar();
            con.Close();
            return obj;
        }
        //Cap nhat DataGridView vao CSDL
        public void UpdateView(DataTable table)
        {
            SqlCommandBuilder cb = new SqlCommandBuilder(adap);
            adap.Update(table);
        }
        public void UpdateView1(DataTable table)
        {
            SqlCommandBuilder cb = new SqlCommandBuilder(adap1);
            adap1.Update(table);
        }
        public void UpdateView2(DataTable table)
        {
            SqlCommandBuilder cb = new SqlCommandBuilder(adap2);
            adap2.Update(table);
        }
    }
}
